package com.define.ssh.action.admin;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.define.ssh.bean.Setting;
import com.define.ssh.util.SettingUtil;
import com.define.ssh.util.ToPinYin;

public class SettingAction extends BaseAdminAction {
	private static final long serialVersionUID = 5L;

	private File img1;
	private File img2;
	private File img3;
	private String img1FileName;
	private String img2FileName;
	private String img3FileName;

	private File adminimg1;
	private File adminimg2;
	private File adminimg3;
	private String adminimg1FileName;
	private String adminimg2FileName;
	private String adminimg3FileName;

	Setting setting;

	/****
	 * 系统参数 读取配置文件
	 * 
	 * @return
	 */
	public String edit() {
		setting = SettingUtil.getSetting();
		return INPUT;
	}

	/****
	 * 保存系统参数
	 * 
	 * @return
	 * @throws IOException
	 */
	public String save() throws IOException {
		final String img1path = saveimg(img1, img1FileName);
		if (!img1path.equals("")) {
			setting.setImg1(img1path);
		}

		final String img2path = saveimg(img2, img2FileName);
		if (!img2path.equals("")) {
			setting.setImg2(img2path);
		}

		final String img3path = saveimg(img3, img3FileName);
		if (!img3path.equals("")) {
			setting.setImg3(img3path);
		}

		final String adminimg1path = saveimg(adminimg1, adminimg1FileName);
		if (!adminimg1path.equals("")) {
			setting.setAdminimg1(adminimg1path);
		}

		final String adminimg2path = saveimg(adminimg2, adminimg2FileName);
		if (!adminimg2path.equals("")) {
			setting.setAdminimg2(adminimg2path);
		}

		final String adminimg3path = saveimg(adminimg3, adminimg3FileName);
		if (!adminimg3path.equals("")) {
			setting.setAdminimg3(adminimg3path);
		}

		SettingUtil.writeSetting(setting);
		return edit();
	}

	public String saveimg(File img, String imgFileName) throws IOException {
		String re = "";
		final String realpath = ServletActionContext.getServletContext()
				.getRealPath("/upload/img");

		if (img != null && imgFileName != null) {
			imgFileName = ToPinYin.toPinYin(imgFileName);
			final File savefile = new File(new File(realpath), imgFileName);
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			FileUtils.copyFile(img, savefile);
			re = "/upload/img/" + imgFileName;
		}
		return re;
	}

	public Setting getSetting() {
		return setting;
	}

	public void setSetting(Setting setting) {
		this.setting = setting;
	}

	public File getImg1() {
		return img1;
	}

	public void setImg1(File img1) {
		this.img1 = img1;
	}

	public File getImg2() {
		return img2;
	}

	public void setImg2(File img2) {
		this.img2 = img2;
	}

	public File getImg3() {
		return img3;
	}

	public void setImg3(File img3) {
		this.img3 = img3;
	}

	public File getAdminimg1() {
		return adminimg1;
	}

	public void setAdminimg1(File adminimg1) {
		this.adminimg1 = adminimg1;
	}

	public File getAdminimg2() {
		return adminimg2;
	}

	public void setAdminimg2(File adminimg2) {
		this.adminimg2 = adminimg2;
	}

	public File getAdminimg3() {
		return adminimg3;
	}

	public void setAdminimg3(File adminimg3) {
		this.adminimg3 = adminimg3;
	}

	public String getImg1FileName() {
		return img1FileName;
	}

	public void setImg1FileName(String img1FileName) {
		this.img1FileName = img1FileName;
	}

	public String getImg2FileName() {
		return img2FileName;
	}

	public void setImg2FileName(String img2FileName) {
		this.img2FileName = img2FileName;
	}

	public String getImg3FileName() {
		return img3FileName;
	}

	public void setImg3FileName(String img3FileName) {
		this.img3FileName = img3FileName;
	}

	public String getAdminimg1FileName() {
		return adminimg1FileName;
	}

	public void setAdminimg1FileName(String adminimg1FileName) {
		this.adminimg1FileName = adminimg1FileName;
	}

	public String getAdminimg2FileName() {
		return adminimg2FileName;
	}

	public void setAdminimg2FileName(String adminimg2FileName) {
		this.adminimg2FileName = adminimg2FileName;
	}

	public String getAdminimg3FileName() {
		return adminimg3FileName;
	}

	public void setAdminimg3FileName(String adminimg3FileName) {
		this.adminimg3FileName = adminimg3FileName;
	}

}
